<?php
/**
 * Created by PhpStorm.
 * User: lzy
 * Date: 2018/11/19
 * Time: 18:13
 */
namespace App\FreeBird\Repositories;
use App\FreeBird\Contracts\NavigationInterface;
use App\FreeBird\Models\NavigationModel;
use Illuminate\Support\Facades\Request;

class  DbNavigationRepositories implements NavigationInterface
{
    /**
     * 模型
     * @return NavigationModel
     */
    public function getNavigationModel(){
        return app()->make(NavigationModel::class);
    }

    /**
     * 列表
     * @param $where
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|mixed
     */
    public function getList($where){
        $request=Request::instance();
        $limit=empty($request->input("limit"))?10:$request->input("limit");
        $sort=empty($request->input("sort"))?'create_time':$request->input("sort");
        $order=empty($request->input("order"))?'desc':$request->input("order");
        return $this->getNavigationModel()->newQuery()->where($where)->orderBy($sort,$order)->paginate($limit);
    }

    /**
     * 保存数据
     * @param $data
     * @return mixed|void
     */
    public function saveData($data){
        $data['id']=empty($data['id'])?null:$data['id'];
        if(empty($data['status'])){
            //$data['status']=0;
        }
        $data["uid"]=AUID;
        return $this->getNavigationModel()->newQuery()->updateOrCreate(["id"=>$data['id']],$data);
    }

    /**
     * 获取单条信息
     * @param $id
     * @return mixed|void
     */
    public function getInfo($id){
        return $this->getNavigationModel()->newQuery()->where("id",$id)->first();
    }

    /**
     * 删除数据
     * @param $where
     * @return mixed
     */
    public function deleteData($where){
        return $this->getNavigationModel()->newQuery()->where($where)->delete();
    }
}